﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoCode
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.AutoScroll = true;

            int dX = 15, dY = 15;
            int x = dX, y = dY;
            int w = 150, h = 80;
            int pW = this.ClientSize.Width;
            foreach (var t in this.GetType().Module.GetTypes())
            {
                if (t.Name != "Main" && GetBaseTypeForForm(t) != null)
                {
                    Button b = new Button()
                    {
                        Text = t.Name,
                        Tag = t,
                        Height = h,
                        Width = w,
                        Bounds = new Rectangle(new Point(x, y), new Size(w, h)),
                        Font = new System.Drawing.Font("微软雅黑", 18),
                        FlatStyle = System.Windows.Forms.FlatStyle.Flat,
                        Location = new Point(x, y)
                    };
                    b.FlatAppearance.BorderSize = 0;
                    b.Click += (obj, ea) =>
                    {
                        var type = b.Tag as Type;
                        (type.Assembly.CreateInstance(type.FullName) as Form).Show();
                    };
                    this.Controls.Add(b);

                    x += w + 5; y += h + 5;

                    if (x + w + 5 + dX > pW)
                    {
                        x = dX;
                    }
                    else
                    {
                        y -= h + 5;
                    }
                }
            }
        }
        Type FormType = typeof(Form);
        Type ObjectType = typeof(Object);
        Type GetBaseTypeForForm(Type t)
        {
            if (t == FormType)
            {
                return t;
            }
            if (t == ObjectType)
            {
                return null;
            }
            if (t == null)
            {
                return null;
            }
            return GetBaseTypeForForm(t.BaseType);
        }
    }
}
